package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.StringUtils;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @className: RoleServiceImpl
 * @description:
 * @author: InnovatorX
 * @date: 2024/6/2 21:58
 * @Version: 1.0
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> findRolePageResponse(int pageNum, int pageSize, String roleDto) {
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<RoleVo> roleVos = roleMapper.findRolePageResponse(roleDto);
        // 获取分页数据
        Page<RoleVo> page = (Page<RoleVo>) roleVos;
        // 将分页数据转换为分页响应对象
        return PageResponse.of(page, RoleVo.class);
    }

    @Override
    public void saveRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 角色 + 资源 = 中间表
     *
     * @param roleId
     * @return
     */
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        return roleResourceMapper.findCheckedResources(roleId);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleDto roleDto) {
        // 更新角色名字
        if (StringUtils.isNotBlank(roleDto.getRoleName())){
            Role role = new Role();
            role.setId(roleDto.getId());
            role.setRoleName(roleDto.getRoleName());
            role.setDataState(roleDto.getDataState());
            roleMapper.updateByPrimaryKey(role);
        } else {
            // 根据RoleID删除中间表的数据
            roleResourceMapper.deleteByROleId(roleDto.getId());
            // 把新的数据插入到中间表中
            String[] arr = roleDto.getCheckedResourceNos();
            List<RoleResource> list = new ArrayList<>();
            for (String resourceNo : arr) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleDto.getId());
                roleResource.setDataState("0");
                roleResource.setResourceNo(resourceNo);
                // 将resourceNo封装到list集合中
                list.add(roleResource);
            }
            roleResourceMapper.batchInsert( list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long roleIds) {
        // 删除中间表数据
        roleResourceMapper.deleteByROleId(roleIds);
        //删除角色数据
        roleMapper.deleteByPrimaryKey(roleIds);
    }

    /**
     *  角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> initRoles() {
        List<Role> roleList = roleMapper.initRoles();
        return BeanUtil.copyToList(roleList,RoleVo.class);
    }
}
